"""
Case Type   : security
Case Name   : sm4算法多次加密同一数据，预期可以随机生成密文
Create At   : 2025/5/9
Owner       : wan005
Description :
    1.gs_encryp函数使用sm4算法多次加密同一数据；
Expect      :
    1.expect:每次生成的密文不一样
History     :
"""
import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Security(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'---{os.path.basename(__file__)} start---')
        self.sh_primy = CommonSH()
        self.common = Common()
        self.sm4_param = 'hello world'

    def test_security(self):
        text = '---step1:gs_encryp函数使用sm4算法多次加密同一数据;expect:密文不一样--'
        self.logger.info(text)
        sql_cmd = f"select gs_encrypt('hello world','1234@abc','sm4');"
        res1 = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(res1)
        row1, _ = self.common.parse_sql_query_res(res1)

        res2 = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(res1)
        row2, _ = self.common.parse_sql_query_res(res2)

        res3 = self.sh_primy.execut_db_sql(sql_cmd)
        self.logger.info(res3)
        row3, _ = self.common.parse_sql_query_res(res3)
        self.assertTrue(row1 != row2 and row1 != row3 and row2 != row3,
                        text + '执行失败')
        self.assertTrue('ERROR' not in row1 and 'ERROR' not in row2 and
                        'ERROR' not in row3, text + '执行失败')

    def tearDown(self):
        self.logger.info(f'---{os.path.basename(__file__)} end---')
